﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.Common;
using weikebuluo.DAL;
using weikebuluo.Model;

namespace weikeweixin.web.admin.website
{
    public partial class quick : weikeweixin.web.admin.cs.adminbase
    {

        JsonOperate JsonOperate = new JsonOperate();
        t_menu_dal ShortcutMenuDAL = new t_menu_dal();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(user_id))
            {
                Response.Write("<script Language=\"Javascript\">alert(\"请先配置微官网\")");
                Response.Write("</script>");
                Response.End();
            }
            switch (Request.QueryString["action"])
            {
                case "list":
                    GetList();
                    break;
                case "del":
                    Delete();
                    break;
                case "display":
                    display();
                    break;
                case "save":
                    Save();
                    break;
                case "button":
                    Button();
                    break;
            }
        }

        private void Save()
        {
            System.Data.DataTable dt = ShortcutMenuDAL.GetList("user_id=" + user_id + " and Type='Button'").Tables[0];
            t_menu_model ShortcutMenuModel = new t_menu_model();
            ShortcutMenuModel.BgColor = Request.Form["namecolor"];
            ShortcutMenuModel.Sort = Request.Form["homemenu"].ToString().Trim();
            ShortcutMenuModel.Type = "Button";
            ShortcutMenuModel.Type2 = "Button";
            ShortcutMenuModel.Status = 1;
            ShortcutMenuModel.user_id = int.Parse(user_id);
            if (dt.Rows.Count > 0)
            {
                ShortcutMenuModel.Id = int.Parse(dt.Rows[0]["Id"].ToString().Trim());
                bool re = ShortcutMenuDAL.Update(ShortcutMenuModel);
                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                }
            }
            else
            {
                int re= ShortcutMenuDAL.Add(ShortcutMenuModel); 
                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                }
            }
            Response.End();
        }

        private void display()
        {
            string key = Request.Form["key"];
            string value = Request.Form["value"];
            int re= ShortcutMenuDAL.Update(int.Parse(key), string.IsNullOrEmpty(value)?"0":"1");
            if (re > 0)
            {
                Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"quick.aspx\",\"error\":\"保存成功\"}");
            }
            else
            {
                Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"quick.aspx\",\"error\":\"保存失败\"}");
            }
            Response.End();
        }

        private void Delete()
        {
            bool bl = ShortcutMenuDAL.Delete(int.Parse(Request.QueryString["id"].Trim()));
        }

        private void GetList()
        {
            Response.Write(JsonOperate.TableToBasicJson(ShortcutMenuDAL.GetList("user_id=" + user_id + " and Type='Menu'").Tables[0]));
            Response.End();
        }

        private void Button()
        {
            Response.Write(JsonOperate.TableToBasicJson(ShortcutMenuDAL.GetList("user_id=" + user_id + " and Type='Button'").Tables[0]));
            Response.End();
        }
    }
}